package com.xuequ.answerme.account;


import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.xuequ.answerme.R;
import com.xuequ.answerme.beans.UserBean;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Shijie on 2015/9/30.
 *
 */
public class JoinFragment extends Fragment {

    private LinearLayout joinCodeLinear;
    private TextInputLayout emailTextInput;
    private Button getCodeButton;

    private LinearLayout joinConfirmLinear;
    private TextInputLayout usernameTextInput;
    private TextInputLayout passwordTextInput;
    private TextInputLayout codeTextInput;
    private Button registerButton;


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_join, container,false);
        initViews(view);

        return view;
    }

    private void initViews(View view) {

        joinCodeLinear = (LinearLayout) view.findViewById(R.id.join_code_linear);
        emailTextInput = (TextInputLayout) view.findViewById(R.id.join_code_email_textInput);
        getCodeButton = (Button) view.findViewById(R.id.join_code_get_button);
        getCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(emailTextInput.getEditText().getText().toString())){
                    checkIsEmailVerified(emailTextInput.getEditText().getText().toString());
                }else{
                    emailTextInput.setError("plz enter an email :)");
                }
            }
        });

        joinConfirmLinear = (LinearLayout) view.findViewById(R.id.join_confirm_linear);
        usernameTextInput = (TextInputLayout) view.findViewById(R.id.join_confirm_username_textInput);
        passwordTextInput = (TextInputLayout) view.findViewById(R.id.join_confirm_password_textInput);
        codeTextInput = (TextInputLayout) view.findViewById(R.id.join_confirm_code_textInput);
        registerButton = (Button) view.findViewById(R.id.join_confirm_register_button);
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(usernameTextInput.getEditText().getText().toString())){
                    usernameTextInput.setError("plz enter a username :)");
                }else if(TextUtils.isEmpty(passwordTextInput.getEditText().getText().toString())){
                    passwordTextInput.setError("plz enter the password :)");
                }else{
                    UserBean newUser = new UserBean();
                    newUser.setUsername("abc");
                    newUser.setPassword("234");
                    newUser.signUp(getActivity(), new SaveListener() {
                        @Override
                        public void onSuccess() {
                            Log.i("JoinFragment","register success!!");
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            Log.i("JoinFragment","register failure!! " + s);
                            if(s.contains("already taken")){
                                usernameTextInput.setError("the username has been used :(");
                                usernameTextInput.getEditText().setText(null);
                                passwordTextInput.getEditText().setText(null);
                            }
                        }
                    });
                }
            }
        });

    }

    private void checkIsEmailVerified(String email) {

    }

}

